<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Treatment;
use App\Models\TreatmentCategory;
use Illuminate\Http\Request;

class TreatmentController extends Controller
{
    protected $treatmentModel;
    protected $treatmentCategoryModel;

    public function __construct()
    {
        $this->treatmentModel= new Treatment();
        $this->treatmentCategoryModel = new TreatmentCategory();
    }

    /**
     * 获取疗程信息(详情页)
     */
    public function treatmentForDetailPage(Request $request){
        $name = $request->input('name');
        $treatment = $this->treatmentModel->getTreatment('name', $name);
        return $this->success($treatment,"获取疗程信息成功");
    }

    /**
     * 获取首页的所有疗程信息
     * @return mixed
     */
    public function treatmentListForHome(){

            //获取所有的疗程分类
            $category = $this->treatmentCategoryModel->getInfoList();
            $arr=[];
            foreach($category as $key => $val){
                //开启是否显示首页的刷选
                $treatmentList = $this->treatmentModel->getTreatmentList('treatment_categories_id',$val['id'],true)->toArray();

                if($treatmentList){
                    $arr[$val['name']] = $treatmentList;
                }
            }

            return $this->success($arr,"各疗程信息获取成功");
    }

    /**
     * 根据分类名获取疗程列表（列表页）
     * @param Request $request
     * @return mixed
     */
    public function treatmentListByCategoryName(Request $request){
        $categoryName = $request->get('categoryName');

        if($categoryName == "所有疗程"){
            $res = $this->treatmentModel->getTreatmentList();
        }else{
            $categoryID = $this->treatmentCategoryModel->getInfo('name',$categoryName)['id'];
            $res = $this->treatmentModel->getTreatmentList('treatment_categories_id',$categoryID);
        }

        return $this->success($res,'获取疗程列表');
    }

    /**
     * 根据搜索词获取疗程列表（列表页）
     */
    public function treatmentListBySearchVal(Request $request){
        $searchVal = $request->get('searchVal');

        $res = $this->treatmentModel->searchTreatmentList('name',$searchVal);

        return $this->success($res,"获取疗程列表成功");
    }

    /**
     * 获取全部疗程名称（预约页）
     * @return mixed
     */
    public function treatmentNameListForBookPage(Request $request){
        $treatment = $request->input('treatment');

        $res = $this->treatmentModel->getTreatmentList();
        $newRes = [];

        foreach ($res as $k=>$v){
            if($treatment == $v['name']){

                unset($newRes[$k]);
                array_unshift($newRes,$v['name']);

            }else{
                $newRes[] = $v['name'];
            }

        }




        return $this->success($newRes,"获取全部疗程成功");
    }
}
